package com.yjc.drygoods.product.web;

import com.yjc.drygoods.product.vo.Catelog2Vo;
import com.yjc.drygoods.product.entity.CategoryEntity;
import com.yjc.drygoods.product.service.CategoryService;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author yjc
 * @create 2022-09-09 10:36
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;


    @GetMapping({"/", "/index.html"})
    public String goIndexPage(Model model) {

        List<CategoryEntity> categoryEntities = categoryService.getCategoryLevel1();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<Long, List<Catelog2Vo>> getCategory2And3() {
        Map<Long, List<Catelog2Vo>> mapRes = categoryService.getCategory2And3();
        return mapRes;
    }


    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");
        door.trySetCount(5L);
        door.await();
        return "人都走了，关门";
    }

    @ResponseBody
    @GetMapping("/leave/{number}")
    public String leave(@PathVariable("number") Long number) throws InterruptedException {
        RCountDownLatch door = redisson.getCountDownLatch("door");

        //计数减一
        door.countDown();
        return number + "班学生走光了";
    }


    @ResponseBody
    @GetMapping("/park")
    public String park() throws InterruptedException {
        RSemaphore park = redisson.getSemaphore("park");
        park.acquire();
        return "停车成功";
    }

    @ResponseBody
    @GetMapping("/go")
    public String go() {
        RSemaphore park = redisson.getSemaphore("park");
        park.release();
        return "离开停车场";
    }


    @ResponseBody
    @GetMapping("/write")
    public String writeValue() {

        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        String uuid = "";
        try {
            rLock.lock();
            System.out.println("修改数据...");
            uuid = UUID.randomUUID().toString();
            Thread.sleep(15000);
            redisTemplate.opsForValue().set("writeValue", uuid);
        } catch (Exception e) {

        } finally {
            rLock.unlock();
        }
        return uuid;
    }

    @ResponseBody
    @GetMapping("/read")
    public String readValue() {
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        String res = "";
        try {
            rLock.lock();
            System.out.println("读取数据中...");
            Thread.sleep(20000);
            res = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {

        } finally {
            rLock.unlock();
        }
        return res;
    }


}
